package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CheckGroupService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        if (queryPageBean != null){
            PageResult pageResult = setmealService.findPage(queryPageBean);
            if (pageResult != null){
                return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,pageResult);
            }else {
                return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
            }
        }

        return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        if (id != null && id >0){
            Setmeal setmeal = setmealService.findById(id);
            if (setmeal != null){
                return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
            }else {
                return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
            }
        }
        return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
    }

    /**
     * 新增setmeal
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @PostMapping("/add")
    public Result addSetMeal(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        if (setmeal != null && setmeal.getId() == null){
            try {
                Integer setId = setmealService.addSetmeal(setmeal, checkgroupIds);
                if (setId !=null && setId>0){
                    return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
                }
            }catch (RuntimeException e){
                return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
            }
        }


        return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
    }


}
